package org.example.mynote.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.example.mynote.Entity.Notebook;
import org.example.mynote.dto.NotebookRequestDTO;
import org.example.mynote.service.NotebookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/notebooks")
@Slf4j
@Tag(name = "笔记本项目相关接口")
@SecurityRequirement(name = "BearerAuth")
//@SecurityRequirement(name = "BearerAuth")// 对整个 Controller 的所有接口应用认证要求
public class NotebookController {

    @Autowired
    private NotebookService notebookService;

    /**
     * (3) 新建项目
     */
    @PostMapping
    @Operation(summary = "新建一个笔记本")
    public ResponseEntity<Notebook> createNotebook(@RequestBody NotebookRequestDTO request) {
        // NotebookCreateRequest 是一个DTO，只有一个 String name 字段
        Notebook createdNotebook = notebookService.createNotebook(request.getName());
        return ResponseEntity.ok(createdNotebook);
    }

    /**
     * (4) 修改项目
     */
    @PutMapping("/{id}")
    @Operation(summary = "根据ID修改笔记本名称")
    public ResponseEntity<Notebook> updateNotebook(@PathVariable Long id, @RequestBody NotebookRequestDTO request) {
        // NotebookUpdateRequest 是一个DTO，只有一个 String newName 字段
        Notebook updatedNotebook = notebookService.updateNotebook(id, request.getName());
        return ResponseEntity.ok(updatedNotebook);
    }

    /**
     * (5) 删除项目
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "根据ID删除笔记本（及其下所有笔记）")
    public ResponseEntity<Void> deleteNotebook(@PathVariable Long id) {
        notebookService.deleteNotebook(id);
        return ResponseEntity.noContent().build(); // 删除成功，返回 204 No Content
    }

    @GetMapping
    public ResponseEntity<List<Notebook>> getAllNotebooks() {
        List<Notebook> notebooks = notebookService.getNotebooksForCurrentUser();
        return ResponseEntity.ok(notebooks);
    }
}
